import { $ } from "./Game";

const keyBind = {
    KEY_DOWN: [
        {
            key: Laya.Keyboard.W,
            event() {
                this.jump();
            },
        },
        {
            key: Laya.Keyboard.S,
            event() {
                this.changeState("down");
            },
        },
    ],
    KEY_UP: [
        {
            key: Laya.Keyboard.S,
            event() {
                this.changeState("up");
            },
        },
    ],
};
const stateChange = {
    up() {
        this.source = "tex/dragon_1.png,tex/dragon_2.png";
        this.height = this.origin.height;
        this.pos(this.x, this.y - this.origin.height / 2);
        this.BoxCollider.height = this.origin.height;
        this.play();
    },
    down() {
        this.source = "tex/dragon_3.png,tex/dragon_4.png";
        this.BoxCollider.height = this.origin.height / 2;
        this.pos(this.x, this.y + this.origin.height / 2);
        this.height = this.origin.height / 2;
        this.play();
    },
};
export default class Tex extends Laya.Animation {
    origin: {
        x: number;
        y: number;
        height: number;
    };
    constructor() {
        super();
        ["KEY_DOWN", "KEY_UP"].forEach((name) => {
            keyBind[name].forEach(({ event, key }) => {
                Laya.stage.on(Laya.Event[name], this, (e) => {
                    if (e["keyCode"] === key) event.call(this, e);
                });
            });
        });
    }
    state = "idle";
    reset(): void {
        this.pos(this.origin.x, this.origin.y);
        this.RigidBody.linearVelocity = {
            x: 0,
            y: 0,
        };
    }
    changeState(state: string) {
        if (state === this.state) {
            return;
        } else {
            stateChange[state].call(this, state);
            this.state = state;
        }
    }

    jumping: boolean = false;
    changeJump(isJumping) {
        this.jumping = isJumping;
        console.log(this.jumping);
    }
    async jump() {
        if (!this.jumping) {
            console.log("飞起来咯");
            this.jumping = true;
            this.RigidBody.linearVelocity = {
                x: 0,
                y: -$.force,
            };
        }
    }
    RigidBody: Laya.RigidBody;
    BoxCollider: Laya.BoxCollider;
    onAwake(): void {
        this.origin = { x: this.x, y: this.y, height: this.height };
        this.RigidBody = this.getComponent(Laya.RigidBody);
        this.BoxCollider = this.getComponent(Laya.BoxCollider);
        this.changeState("up");
    }
    onStart(): void {}
    onDisable(): void {}
    onUpdate(): void {}
}
